package com.cms.facade;

import java.sql.Connection;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import com.cms.bean.MenuBean;
import com.cms.bean.SysConfigBean;
import com.cms.bean.UserBean;
import com.cms.config.ConfigDef;
import com.cms.database.DBHelper;
import com.cms.menu.MenuHelper;
import com.cms.util.StringHelper;

public class SessionFacade {

	public static SysConfigBean getSysConfigBean(HttpSession session) {
		Object obj = session.getAttribute(HttpAttribute.SYS_CONFIG);
		if (obj == null) {
			return null;
		} else {
			return (SysConfigBean) obj;
		}
	}

	public static void setSysConfigBean(HttpSession session,
			SysConfigBean sysconfigbean) {
		session.setAttribute(HttpAttribute.SYS_CONFIG, sysconfigbean);
	}

	public static long getVisitTime(HttpSession session) {
		Object obj = session.getAttribute(HttpAttribute.VISIT_TIME);
		if (obj == null) {
			return 0;
		} else {
			return (Long) obj;
		}
	}

	public static void setVisitTime(HttpSession session, long time) {
		session.setAttribute(HttpAttribute.VISIT_TIME, time);
	}
	

	public static String getUser(HttpSession session) {
		Object obj = session.getAttribute(HttpAttribute.USER);
		if (obj == null) {
			return "";
		} else {
			return String.valueOf(obj);
		}
	}

	public static void setUser(HttpSession session, String user) {
		session.setAttribute(HttpAttribute.USER, user);
	}
	
	public static Locale getLocale(HttpSession session) {
		return (Locale) session.getAttribute(HttpAttribute.LOCALE);
	}

	public static void setLocale(HttpSession session, Locale locale) {
		session.setAttribute(HttpAttribute.LOCALE, locale);
	}

	public static UserBean getUserBean(HttpSession session) {
		Object obj = session.getAttribute(HttpAttribute.USER_BEAN);
		if (obj == null) {
			return null;
		} else {
			return (UserBean)obj;
		}
	}

	public static void setUserBean(HttpSession session, UserBean user) {
		session.setAttribute(HttpAttribute.USER_BEAN, user);
	}

	public static boolean isSysAdmin(HttpSession session) {
		Object obj = session.getAttribute(HttpAttribute.IS_SYS_ADMIN);
		if (obj == null) {
			return false;
		} else {
			return (Boolean)obj;
		}
	}

	public static void setIsSysAdmin(HttpSession session, String admin_type) {
		boolean flag = false;
		if (!StringHelper.isEmpty(admin_type)&&admin_type.equals(ConfigDef.SYS_ADMIN)){
			flag = true;
		}
		session.setAttribute(HttpAttribute.IS_SYS_ADMIN, flag);
	}
	
	public static MenuBean getRootMenuBean(HttpSession session){
		MenuBean root = (MenuBean)session.getAttribute(HttpAttribute.ROOT_MEUN);
		if(root==null){
			Connection conn = DBHelper.getConn();
			root = MenuHelper.getRootMenuBean(conn);
			DBHelper.closeConn(conn);
		}
		
		return root;
	}
	
}
